﻿namespace ScrumLite.WebUx.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ScrumLite.Services;
    using ScrumLite.WebUx.Controllers;
    using ScrumLite.WebUx.Models;
    using Rhino.Mocks;

    [TestClass]
    public class ContextBaseControllerFixture
    {
        [TestMethod]
        public void ShouldCreateContextViewDataIfNoViewDataAvailable()
        {
            MockProfileService profile = new MockProfileService();
            TestableController controller = new TestableController(profile);
            MockHelpers.SetupMocks(controller);

            controller.ActionWithNoViewData();

            MockView mockFactory = ((MockView)controller.ViewEngine);
            Assert.IsInstanceOfType(mockFactory.ViewData, typeof(ContextViewData));
            Assert.IsTrue(profile.GetFullNameCalled);
        }

        [TestMethod]
        public void ShouldSetContextViewDataProperties()
        {
            TestableController controller = new TestableController();
            MockHelpers.SetupMocks(controller);

            controller.ActionWithViewData();

            MockView mockFactory = ((MockView)controller.ViewEngine);
            Assert.AreEqual("Foo", ((ContextViewData)mockFactory.ViewData).UserName);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldThrowIfViewDataDoesNotInheritFromContextViewData()
        {
            TestableController controller = new TestableController();
            MockHelpers.SetupMocks(controller);

            controller.ActionWithWrongViewData();
        }        
    }

    class TestableController : ContextBaseController
    {
        public TestableController()
            : base(new MockProfileService())
        {
        }

        public TestableController(IProfileService profile) : base(profile)
        {
        }

        public void ActionWithNoViewData()
        {
            RenderView("Test");
        }

        public void ActionWithViewData()
        {
            RenderView("Test", new MockViewData());
        }

        public void ActionWithWrongViewData()
        {
            RenderView("Test", new { Foo = "Bar"} );
        }
    }
    
    class MockViewData : ContextViewData
    {
    }
}
